<?php
//include_once "session.php";
include_once "db.php";

use DataBase\Db;

// new  实例化  对象
class Dcservice
{
    // 属性
    private $conn;
    public $conn2;
    // private 私有的 只能对象自己使用 $this->
    // public  公有的 任何地方都可用

    // 构造函数
    function __construct()
    {
        $db = new DataBase\Db();
        $this->conn = $db->connet();
    }


    // 方法
    function dc($boardId, $dishId)
    {
        if (isset($boardId) && isset($dishId)) {
            $sql = "select * from dish_order where board_id=$boardId and `status`='未结账';";
            $sqly = $this->conn->prepare($sql);
            $ok = $sqly->execute();
            if (!$ok) {
                throw new Error('查询订单失败');
            }
            $order = $sqly->fetch();
            $_SESSION["order"] = $order;
            $order_id = $order['id'];

            $sql = "select * from dish where id=$dishId";
            $sqlu = $this->conn->prepare($sql);
            $ok = $sqlu->execute();
            $dish = $sqlu->fetch();
            $price = $dish['price'];

            $sql = "insert into order_item(dish_id,order_id,price) values ($dishId,$order_id,$price)";
            $sqli = $this->conn->prepare($sql);
            $ok = $sqli->execute();

            $sql = "update dish_order set money=money+$price where id=$order_id";
            $sqlo = $this->conn->prepare($sql);
            $ok = $sqlo->execute();
            return ['order' => $order, 'dish' => $dish];
        }
    }

    function dl($n, $p)
    {
        $msg = null;
        if (isset($n) && empty($n)) {
            $msg .= "用户名不能为空；";
        }

        if (isset($p) && empty($p)) {
            $msg .= "密码不能为空；";
        }

        if (isset($n) && isset($p) && !empty($n) && !empty($p)) {
            $sql = "select * from user where user_name='$n'";
            $sqls = $this->conn->prepare($sql);
            $ok = $sqls->execute();

            if ($ok) {
                $item = $sqls->fetch();
                if ($item != false) {
                    if ($item['password'] == $p) {
                        $_SESSION["user"] = $item;
                        return null;
                    } else {
                        $msg .= "密码错误；";
                    }

                } else {
                    $msg .= "用户名不存在；";
                }

            }
        }
        return $msg;
    }

    function getBoards($board_name)
    {
        $sql = "select * from board";
        if (isset($board_name)) {
            $sql = "select * from board where `name` like '%$board_name%'";
        }
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();
        $item = $sqlo->fetchAll();
        return $item;
    }

    function getboard()
    {
        $sql = "select * from board order by id";
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();
        $list = $sqlo->fetchAll();
        return $list;
    }

    function getorder($boardId)
    {

        if (isset($boardId)) {
            $sql = "select *,dish_order.id as oid from dish_order join board on board.id =dish_order.board_id where board.id=$boardId and dish_order.`status`='未结账'";
            $sqlq = $this->conn->prepare($sql);
            $ok = $sqlq->execute();
            $order = $sqlq->fetch();
            // [ 'name'=>'asdf' , 'oid'=>12 ,....]


            $boardName = $order['name'];//  'asdf'

            if (empty($order)) {
                $sql = "insert into dish_order(board_id,money,create_date,`status`) values($boardId,0,now(),'未结账');select LAST_INSERT_ID() as id;";
                $sqle = $this->conn->prepare($sql);
                $ok = $sqle->execute();
                $relust = $sqle->fetch();

                $order_id = $relust['id'];
            } else {
                $order_id = $order['oid'];
            }

            $sql = "select *,order_item.id as oid from order_item join dish on order_item.dish_id=dish.id where order_item.order_id=$order_id";
            $sqlr = $this->conn->prepare($sql);
            $ok = $sqlr->execute();
            $dishs = $sqlr->fetchAll();

            // [
            //      0=>['name'=>'asdf' , 'oid'=>12 ,....],
            //      1=>['name'=>'asdfasdf' , 'oid'=>12 ,....]
            // ]

            // 字符串类型 . 字符串类型  √

            // 字符串类型 . 数组类型   ×
            return ["boardName" => $boardName, "dishs" => $dishs, "orderId" => $order_id];
        }
    }

    function getjz($order_id)
    {
        $sql = "select *,order_item.id as oid from order_item join dish on order_item.dish_id=dish.id where order_id=$order_id";
        $sqlq = $this->conn->prepare($sql);
        $ok = $sqlq->execute();
        $item = $sqlq->fetchAll();
        return $item;
    }

    function getjz2($order_id)
    {
        $sql = "update dish_order set end_date=now(),status='已结账' where id=$order_id";
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();
    }


    function getleixing()
    {
        $sql = "select * from dish_type order by id asc;";
        $sqlk = $this->conn->prepare($sql);
        $ok = $sqlk->execute();
        $p = $sqlk->fetchAll();
        return $p;
    }

    function getdish($dish_name)
    {
        $sql = "select * from dish";
        if (isset($dish_name)) {
            $sql = "select * from dish where dish_name like '%$dish_name%'";
        }
        $sqlp = $this->conn->prepare($sql);
        $ok = $sqlp->execute();
        $itemo = $sqlp->fetchAll();
        return $itemo;
    }

    function getDel($id)
    {
        $sql = "delete from order_item where id=$id";
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();
    }

    function getDishTypes()
    {
        $sql = "select * from dish_type order by id";
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();
        $item = $sqlo->fetchAll();
        return $item;
    }

    function getDishadd2()
    {
        $sql = "select * from dish order by dish_type_id asc;";
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();
        $item = $sqlo->fetchAll();
        return $item;
    }

    function addboard($boardName)
    {
        $sql = "insert into board(`name`) values ('$boardName')";
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();

        if (empty($boardName)) {
            exit('请添加餐桌');
        }

        if (!isset($ok)) {
            exit('添加失败');
        }
    }

    function addDish($dish_name, $price, $dish_type_id, $file)
    {
        $sql = "insert into dish(dish_name,price,dish_type_id,icon) values ('$dish_name','$price','$dish_type_id','$file')";
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();

        if (empty($dish_name)) {
            exit('请输入菜名');
        }

        if (empty($price)) {
            exit('请输入价格');
        }

        if (empty($dish_type_id)) {
            exit('请插入类型');
        }

        if (empty($file)) {
            exit('请入图片');
        }

        if (!isset($ok)) {
            exit("添加失败");
        }
    }

    function addDishType($type_name)
    {
        $sql = "insert into dish_type(type_name) values ('$type_name')";
        $sqlo = $this->conn->prepare($sql);
        $ok = $sqlo->execute();

        if (empty($type_name)) {
            exit('请插入类型');
        }

        if (!isset($ok)) {
            exit ('插入失败');
        }
    }

    function getLx2()
    {
        $sql = "select * from dish_type order by id asc";
        $sqls = $this->conn->prepare($sql);
        $ok = $sqls->execute();
        $item = $sqls->fetchAll();
        return $item;
    }

    function getdingdan()
    {
        $sql = "select * from dish_order where `status`='未结账' order by board_id asc;";
        $sqls = $this->conn->prepare($sql);
        $ok = $sqls->execute();
        $item = $sqls->fetchAll();
        return $item;
    }

    function getdingdan2()
    {
        $sql = "select * from dish_order join board on board.id=dish_order.board_id where `status`='已结账' order by board_id asc;";
        $sqls = $this->conn->prepare($sql);
        $ok = $sqls->execute();
        $item = $sqls->fetchAll();
        return $item;
    }

    function getxg($username, $oldPwd, $newPwd)
    {
        if (empty($username)) {
            throw new \Exception("请输入用户名");
        }
        if (empty($oldPwd)) {
            throw new \Exception("请输入原密码");

        }
        if (empty($newPwd)) {
            throw new \Exception("请输入修改密码");
        }

        //region 验证原始密码是否相等

        $sql="select * from `user` where user_name='$username'";
        $currentUser = $this->conn->prepare($sql);
        $currentUser->execute();
        $user= $currentUser->fetch();
        if($user['password'] != $oldPwd){
            throw new \Exception("原密码错误");
        }

        //endregion

        if (isset($username)||isset($oldPwd)){
            $sql = "update `user` set `password`='$newPwd' where user_name='$username'";
        }
        $sqls = $this->conn->prepare($sql);
        $ok = $sqls->execute();


        if (!isset($ok)) {
            throw new \Exception("修改失败！请稍后重试。");
        }
    }
}